<?php
/*
Primary Author: Derek Shue
Secondary: Alex Arey
Documentation: Cheng Yi
Updated: 12/10/2011
Module Description: The Community Partner Contacts module adds/finds/updates/deletes Community Partner Contacts from the database.
It also allows for setting/deleting Primary Contacts for Community Partners.
*/
class Application_Model_DbTable_CPCONTACTS extends Zend_Db_Table_Abstract
{

    protected $_name = 'CPCONTACTS';


public function getCPContact($where)
    {
        $row = $this->fetchRow($where);
        if (!$row) {
            return null;
        }
        return $row->toArray();
    }

    public function addCPContact($cpid,$prefix,$firstname,$lastname,$suffix,$gender,$jobtitle,$phone,$extension,$fax,$address1,$address2,$city,$state,$zip,$email)
    {
		$contactid = $this->getNextKey();
		$primarycontact = 'no';
        $data = array(
            'CONTACTID'=> $contactid,
			'CPID'=> $cpid,
			'PREFIX'=> $prefix,
			'FIRSTNAME'=> $firstname,
			'LASTNAME'=> $lastname,
			'SUFFIX'=> $suffix,
			'GENDER'=> $gender,
			'JOBTITLE'=> $jobtitle,
			'PHONE'=> $phone,
			'EXTENSION'=> $extension,
			'FAX'=> $fax,
			'ADDRESS1'=> $address1,
			'ADDRESS2'=> $address2,
			'CITY'=> $city,
			'STATE'=> $state,
			'ZIP'=> $zip,
			'EMAIL'=> $email,
			'PRIMARYCONTACT' => $primarycontact,
			
        );
        $this->insert($data);
    }

	
	public function changePrimaryContact($contactid, $cpid){
		$this->getAdapter()->beginTransaction();
		
		try{
			$row = $this->getCPContact("CPID = " . $cpid .  " AND PRIMARYCONTACT = 'yes'");
			
			if($row){
				$this->dropPrimary($row->CONTACTID);
			}
			
			$this->setPrimary($contactid);
			$this->getAdapter()->commit();
		}
		catch(Exception $e){
			$this->getAdapter()->rollBack();
			echo $e->getMessage();
		}
			
	
	}
	
	public function dropPrimary($contactid){
		
		$data = array(
				'PRIMARYCONTACT' => 'no',
			);
        $this->insert($data, "CONTACTID = " . (int)$contactid);
	
	}
	
	
	
	public function setPrimary($contactid){
		
		$data = array(
				'PRIMARYCONTACT' => 'yes',
			);
			
		echo $contactid;
		$this->update($data, "CONTACTID = " . (int)$contactid);
	
	}

	
	
    public function updateCPContact($contactid,$cpid,$prefix,$firstname,$lastname,$suffix,$gender,$jobtitle,$phone,$extension,$fax,$address1,$address2,$city,$state,$zip,$email)
    {
        $data = array(
			'PREFIX'=> $prefix,
			'FIRSTNAME'=> $firstname,
			'LASTNAME'=> $lastname,
			'SUFFIX'=> $suffix,
			'GENDER'=> $gender,
			'JOBTITLE'=> $jobtitle,
			'PHONE'=> $phone,
			'EXTENSION'=> $extension,
			'FAX'=> $fax,
			'ADDRESS1'=> $address1,
			'ADDRESS2'=> $address2,
			'CITY'=> $city,
			'STATE'=> $state,
			'ZIP'=> $zip,
			'EMAIL'=> $email,
	       );
		print_r($data);
        $this->update($data, 'CONTACTID = '. (int)$contactid . " AND CPID = " . $cpid);
		//echo 'CONTACTID = '. (int)$contactid . " AND CPID = " . $cpid;
    }

	
	
	
    public function deleteCPContact($where)
    {
        $this->delete($where);
    }


	public function getNextKey(){
			$sqlquery = $this->select()->from($this->_name, array("MAX(CONTACTID)"));
			$insertrow = $this->fetchRow($sqlquery)->toArray(); 
			return $insertrow["MAX(CONTACTID)"] + 1;
	
	}





}


















































































































